﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/6/30 星期日 21:10:38
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Sector7.Medical.Utility;
using Sector7.Framework.Utility;
using Sector7.Medical.Consts;
using Sector7.Medical.TRAS;

namespace Sector7.Medical.Biz
{
    /// <summary>
    /// TRAS代理
    /// </summary>
    public class Agent_TRAS
    {
        #region Public functions
        /// <summary>
        /// 下载照片
        /// </summary>
        /// <param name="relativePath">文件相对路径</param>
        /// <returns></returns>
        public static int DownloadFile(string relativePath)
        {
            int result = 0;

            //try
            //{
            //    var inMsg = new TransferMessage();
            //    inMsg.FileName = relativePath;
            //    inMsg.ErrorCode = 0;
            //    inMsg.ErrorMessage = string.Empty;
            //    inMsg.SourceStream = new MemoryStream();

            //    var outMsg = WcfClient.TRAS.Invoke<TransferMessage>(
            //        service => service.DownloadFile(inMsg));

            //    result = outMsg.ErrorCode;
            //    if (result == 0)
            //    {
            //        var sourceStream = outMsg.SourceStream;
            //        if (sourceStream != null)
            //        {
            //            if (sourceStream.CanRead)
            //            {
            //                relativePath = PathUtil.Combine(ApplicationUtil.GetBaseDirectory(), relativePath);
            //                var targetStream = new FileStream(relativePath, FileMode.Create, FileAccess.Write);

            //                var buffer = new byte[C_MaxSize.STREAM_BUFFER_MAX_SIZE];
            //                int count = 0;
            //                do
            //                {
            //                    count = sourceStream.Read(buffer, 0, C_MaxSize.STREAM_BUFFER_MAX_SIZE);
            //                    targetStream.Write(buffer, 0, count);
            //                } while (count > 0);

            //                GCUtil.CloseStream(sourceStream);
            //                GCUtil.CloseStream(targetStream);
            //            }
            //        }
            //    }
            //}
            //catch (Exception e)
            //{

            //}

            return result;
        }

        /// <summary>
        /// 上传照片
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static TransferResult UploadProfile(string citizenId, string filePath)
        {
            var param = new TransferParameter();
            try
            {
                param.DirectoryName = C_PathName.DIR_AVATARS;
                param.FileName = citizenId;
                param.SourceStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                return WcfClient.TRAS.Invoke<TransferResult>(
                    service => service.UploadFile(param));
            }
            finally
            {
                GCUtil.CloseStream(param.SourceStream);                
            }
        }
        #endregion

        #region Private functions
        #endregion
    }
}
